﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class MitarbeiterUebersicht : Form
    {
        public enum sorttype { none = 0, normal, desc };
        public sorttype list = sorttype.none;
        public MitarbeiterUebersicht()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void MitarbeiterUebersicht_Load(object sender, EventArgs e)
        {
            Refresh();
        }
        private void Refresh()
        {
            try
            {
                using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                {
                    con.Open();
                    OracleCommand com = con.CreateCommand();
                    com.CommandText = "SELECT Name, Vorname, Strasse, PLZ, Ort, Telefon, Mail, Benutzername, Passwort,ID FROM Mitarbeiter";

                    OracleDataReader r = com.ExecuteReader();

                    int counter = 0;
                    listView1.Items.Clear();
                    while (r.Read())
                    {
                        listView1.Items.Add(r.GetValue(0).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(4).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(5).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(6).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(7).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(8).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(9).ToString());
                        counter++;
                    }
                }
            }
            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message, "Oracle Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Mitarbeiter m = new Mitarbeiter();
            m.ShowDialog();
            Refresh();
        }

        private void MitarbeiterUebersicht_Load_1(object sender, EventArgs e)
        {
            Refresh();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                    {
                        con.Open();
                        OracleCommand com = con.CreateCommand();
                        com.CommandText = "DELETE FROM Mitarbeiter WHERE ID = " + listView1.SelectedItems[0].SubItems[9].Text + "";

                        com.ExecuteNonQuery();
                    }
                }
                catch (OracleException ex)
                {
                    MessageBox.Show(ex.Message, "Oracle Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Refresh();
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            try
            {
                using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                {
                    con.Open();
                    OracleCommand com = con.CreateCommand();
                    if (list == sorttype.none)
                    {
                        com.CommandText = "SELECT Name, Vorname, Strasse, PLZ, Ort, Telefon, Mail, Benutzername, Passwort,ID FROM Mitarbeiter ORDER BY Name";
                        list = sorttype.normal;
                    }
                    else if (list == sorttype.normal)
                    {
                        com.CommandText = "SELECT Name, Vorname, Strasse, PLZ, Ort, Telefon, Mail, Benutzername, Passwort,ID FROM Mitarbeiter ORDER BY Name DESC";
                        list = sorttype.desc;
                    }
                    else
                    {
                        com.CommandText = "SELECT Name, Vorname, Strasse, PLZ, Ort, Telefon, Mail, Benutzername, Passwort,ID FROM Mitarbeiter";
                        list = sorttype.none;
                    }
                    OracleDataReader r = com.ExecuteReader();

                    int counter = 0;
                    while (r.Read())
                    {
                        listView1.Items.Add(r.GetValue(0).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(4).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(5).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(6).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(7).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(8).ToString());
                        listView1.Items[counter].SubItems.Add(r.GetValue(9).ToString());
                        counter++;
                    }
                }
            }
            catch (OracleException ex)
            {
                MessageBox.Show(ex.Message, "Oracle Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
